package com.imau.controller;

import com.alibaba.fastjson.JSON;
import com.imau.bean.Brand;
import com.imau.bean.Page;
import com.imau.service.BrandService;
import com.imau.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "BrandAllServlet", value = "/BrandAllServlet")
public class BrandAllServlet extends HttpServlet {


    //服务
    private BrandService service = new BrandServiceImpl();


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置响应类型及编码
        //  response.setHeader("Content-Type","text/html;charset=UTF-8");
        response.setContentType("text/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        String currentPage = request.getParameter("currentPage");

        String pageSize = request.getParameter("pageSize");

        //获取查询条件
        String jsonBrand = request.getReader().readLine();


        Brand brand = JSON.parseObject(jsonBrand, Brand.class);


        //数据准备
        Page<Brand> brandPage = service.queryBypageAndCondition(Long.valueOf(currentPage), Long.valueOf(pageSize), brand);

        //数据转成JSON
        String jsonString = JSON.toJSONString(brandPage);

        //响应到前端

        response.getWriter().write(jsonString);

    }


//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//        //设置响应类型及编码
//        //  response.setHeader("Content-Type","text/html;charset=UTF-8");
//        response.setContentType("text/json;charset=utf-8");
//
//
//        String currentPage = request.getParameter("currentPage");
//
//        String pageSize = request.getParameter("pageSize");
//
//        //数据准备
//        Page<Brand> brandPage = service.queryBypage(Long.valueOf(currentPage), Long.valueOf(pageSize));
//
//        //数据转成JSON
//        String jsonString = JSON.toJSONString(brandPage);
//
//        //响应到前端
//
//        response.getWriter().write(jsonString);
//
//    }

//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
//        //设置响应类型及编码
//      //  response.setHeader("Content-Type","text/html;charset=UTF-8");
//        response.setContentType("text/json;charset=utf-8");
//
//        //数据准备
//        List<Brand> brands = service.queryAll();
//
//        //数据转成JSON
//        String jsonString = JSON.toJSONString(brands);
//
//        //响应到前端
//
//        response.getWriter().write(jsonString);
//
//    }
}
